package com.webzavod.model;

import com.webzavod.model.Authority;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.IndexColumn;

import javax.persistence.*;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: v.tsapaev
 * Date: 25.05.12
 * Time: 19:13
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "users", schema = "public")
public class User {

    @Id
    @Column(name = "user_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    @Column(nullable = false, unique = true)
    @Index(name = "username_ix")
    private String username;

    @Column(nullable = false)
    private String password;

    private Boolean enabled;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER/*, targetEntity = Authority.class*/)
    @JoinTable(
            name="users_authorities",
            joinColumns = @JoinColumn( name="user_id"),
            inverseJoinColumns = @JoinColumn( name="authority_id"),
            uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "authority_id"})}
    )
    private Set<Authority> authority;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Set<Authority> getAuthority() {
        return authority;
    }

    public void setAuthority(Set<Authority> authority) {
        this.authority = authority;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", username='" + username + '\'' +
                ", password='[protected]'" +
                ", enabled=" + enabled +
                ", authority=" + authority +
                '}';
    }
}
